<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roleList" >
      <el-table-column label="ID" prop="id" align="center">
        <template #default="{row}">
          <div>
            {{row.id || '--'}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="所属运营商" prop="operator" align="center">
        <template #default="{row}">
          <div>
            {{row.operator || '--'}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="号段前缀" prop="prefix" align="center">
        <template #default="{row}">
          <div>
            {{row.prefix || '--'}}
          </div>
        </template>
      </el-table-column>

      <el-table-column label="状态" prop="status" align="center">
      <template #default="{row}">
        <div>
          <el-tag v-if="row.status==0">不拦截</el-tag>
          <el-tag v-else-if="row.status==1" type="success">拦截</el-tag>
          <div v-else>{{row.status || '--'}}</div>
        </div>
      </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"  width="160">
        <template #default="{row}">
          <span>{{ parseTime(row.createTime) || '--' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
        <template #default="{row}">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="virtualUpdate(row)"
          >更新状态
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <add ref="addRef" @success="getList"/>
  </div>
</template>

<script>
import {MPhonePrefixPage, MPhonePrefixRemove,phoneStaUpdate} from "@/api/user/user"
import add from "./components/add.vue"
export default {
  name: "Role",
  components: {
    add
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询角色列表 */
    async getList() {
      this.loading = true
      let res = await MPhonePrefixPage()
      this.roleList = res.data
      this.loading = false
    },
    // 更新状态
    virtualUpdate(row){
      if(row.status==1){
        this.$confirm('更新当前虚拟号段状态为不拦截?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let data={
            id: row.id,
            status: 0
          }
          await phoneStaUpdate(data)
          this.$message({
            type: 'success',
            message: '更新成功!'
          });
          this.getList()
        })
      }

      if(row.status==0){
        this.$confirm('更新当前虚拟号段状态为拦截?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          let data={
            id: row.id,
            status: 1
          }
          await phoneStaUpdate(data)
          this.$message({
            type: 'success',
            message: '更新成功!'
          });
          this.getList()
        })
      }


    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
     this.$refs.addRef.onOpen()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.addRef.onOpen(row)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.id
      this.$modal.confirm('是否确认删除虚拟号段？').then(function () {
        return MPhonePrefixRemove({id:roleIds})
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
  }
}
</script>
